// pages/function/function.js
let typeONE = ''
let typeTWO = ''
let NM = '0'
let TYPE=[]
let KEY = ''
let T = ''
let page = 0
var allGoods = []
let userlatitude =''
let userlongtitude =''
let S = 0 //筛选的距离
Page({
  data:{
    list:[],
    key:null
  },
  onLoad(options) {
    userlatitude=options.userlatitude
    userlongtitude=options.userlongtitude
    console.log('userlatitude为:',userlatitude)
    console.log('userlongtitude:',userlongtitude)
    console.log('列表携带的值',options)
    typeONE = options.type1
    typeTWO = options.type2
    for (let TN = typeONE; TN <= typeTWO; TN++) {
      TYPE[NM]=TN
      console.log('TYPE['+NM+']为：',TYPE[NM])
      this.ConditionsQuery(TYPE[NM])
      NM++
    }
    console.log('allGoods为：',allGoods)
    NM=0
  },
   //下拉触底
   onReachBottom(){
    console.log('---------------触底执行了----------------')
    if(havetraverse==AllGoodsNum){
      wx.showToast({
        title: '已经到达底部',
        icon:'none'
      })
    } else {
      page++
      console.log('---------------触底判断执行了----------------')
      this.getSkip(allGoods,havetraverse)
    }
    console.log('渲染的页面是：',page)
  },
   //---------------------------------
 Rad: function(d) { //根据经纬度判断距离
    return d * Math.PI / 180.0;
},
getDistance: function(lat1, lng1, lat2, lng2) {
    // lat1用户的纬度
    // lng1用户的经度
    // lat2商家的纬度
    // lng2商家的经度
    var radLat1 = this.Rad(lat1);
    var radLat2 = this.Rad(lat2);
    var a = radLat1 - radLat2;
    var b = this.Rad(lng1) - this.Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    S = Math.round(s * 10000) / 10000
    s = s.toFixed(2) + 'km' //保留两位小数
    S = S.toFixed(2)
    console.log('经纬度计算的距离:' + s)
    return s
},
  getKey(e){
    console.log('搜索的内容是：',e.detail.value)
    this.setData({
      key:e.detail.value
    })
    KEY = e.detail.value
  },
  goSearch(e){
    if(!this.data.key){
      wx.showToast({
        title: '请输入内容',
        icon:'none'
      })
    }else{
    console.log('点击了搜索,跳转页面携带的KEY为:',KEY)
    wx.navigateTo({
      url: '/pages/Search/Search?SearchKey='+KEY+'&T='+2+'&userlatitude='+userlatitude+'&userlongtitude='+userlongtitude+'&array='+encodeURIComponent(JSON.stringify(allGoods)),
    })
  }
  },
  onReachBottom:function(){
    page++
    this.Pagingload(page)
    console.log('渲染的页面是：',page)
  },
  ConditionsQuery(type){
    wx.cloud.database().collection('send-Goods')
    .where({
        type:parseInt(type),
    })
    .get()
    .then(res=>{
      if(res.data.length!=0){
        for (let index = 0; index < res.data.length; index++) {
          const element = res.data[index];
          allGoods.push(element)
          this.getDistance(userlatitude,userlongtitude,element.latitude,element.longitude)
         if(S<=2){
         this.setData({
          list:this.data.list.concat(element)
         })
       }
        }
        // this.setData({
        //   list:this.data.list.concat(res.data)
        // })
        
       }
      console.log('function查询商品成功:',res)
    })
    .catch(err=>{
      console.log('function查询商品失败:',err)
    })
  },
  goDetail(e){
    console.log('点击了商品详情页',e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/details/details?id='+e.currentTarget.dataset.id,
    })
  },
})